package com.lc.guessTheWords.util;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.Toast;

import com.bodong.dianjinweb.DianJinPlatform;
import com.bodong.dianjinweb.listener.AppActiveListener;
import com.bodong.dianjinweb.listener.ConsumeListener;
import com.lc.guessTheWords.MainActivity;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.scene.ToolShopScene;

public class DianJinTool {

	public static String TAG = "DianJoyTool";

	public static void init() {
		// 点金
		DianJinPlatform.initialize(MainActivity.instance, 7246,
				"18df7003c7ce0f0ebef2bb5d06e2cfeb", 1001);
		DianJinPlatform.hideFloatView(MainActivity.instance);

		DianJinPlatform.setAppActivedListener(new AppActiveListener() {

			@Override
			public void onError(int arg0, String arg1) {
				Log.i(TAG, "奖励M币:ERROR");
			}

			@Override
			public void onSuccess(long reward) {
				if (reward > 0) {
					int money = (int) reward;
					// TODO Auto-generated method stub
					Log.i(TAG, "奖励M币:" + String.valueOf(money));
					consume(money);
					// 更新现有的金币数量
					StageManager.instance.setGoldCount(StageManager.instance
							.getGoldCount() + money);
					ToolShopScene.make().updateCoins();
				}
			}
		});
	}

	public static void getBalance() {
		DianJinPlatform.getBalance(MainActivity.instance);
	}

	public static void showOfferWall(final String comfirmTip) {
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (comfirmTip != null) {
					AlertDialog alertDialog = new AlertDialog.Builder(
							MainActivity.instance)
							.setTitle("提示")
							.setMessage(comfirmTip)
							.setPositiveButton("免费赚取",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											DianJinPlatform
													.showOfferWall(MainActivity.instance);
										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											return;
										}
									}).setCancelable(false).create();
					alertDialog.show();
				} else {
					DianJinPlatform.showOfferWall(MainActivity.instance);
				}
			}
		});
	}

	public static void destroy() {
		DianJinPlatform.destory(MainActivity.instance);
	}

	private static void consume(final long value) {
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				DianJinPlatform.consume(MainActivity.instance, value,
						new ConsumeListener() {

							@Override
							public void onError(int errorCode,
									String errorMessage) {
								switch (errorCode) {
								case DianJinPlatform.DIANJIN_ERROR_ILLEGAL_CONSUNE:// 合法的消费金额
									Toast.makeText(MainActivity.instance,
											errorMessage, Toast.LENGTH_SHORT)
											.show();
									break;
								case DianJinPlatform.DIANJIN_ERROR_BALANCE_NO_ENOUGH:// 余额不足
									Toast.makeText(MainActivity.instance,
											errorMessage, Toast.LENGTH_SHORT)
											.show();
								default:
									break;
								}

							}

							@Override
							public void onSuccess() {
								Toast.makeText(MainActivity.instance, "消费成功",
										Toast.LENGTH_SHORT).show();
							}

						});

			}
		});

	}
}
